package com.translate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 */
public class ExcelDataContext {

    private static Map<String,List<List<String>>> threadLocal = new HashMap<>();
    private static Map<String, List<List<String>>> firstLineCache = new HashMap<>();

    public static List<List<String>> getContext(String batchId)
    {
        List<List<String>> dl = threadLocal.get(batchId);
        if(dl==null){
            dl = new ArrayList<>();
            threadLocal.put(batchId, dl);
        }
        return dl;
    }

    public static List<List<String>> getContextList(String batchId) {
        return getContext(batchId);
    }

    public static void addList(String batchId, List<String> dataList){
        getContext(batchId).add(dataList);
    }

    public static void clear(String batchId) {
        threadLocal.remove(batchId);
    }

    public static List<List<String>> getFirstLineCache(String batch) {
        return firstLineCache.get(batch);
    }

    public static void putList(String batchId, List<String> rowlist) {
        List<List<String>> result = firstLineCache.get(batchId);
        if(result == null) {
            result = new ArrayList<>();
            firstLineCache.put(batchId, result);
        }
        result.add(rowlist.stream().collect(Collectors.toList()));
    }

    public static void clearFirstLine(String batch){
        firstLineCache.put(batch,null);
    }

    public static boolean isDone(String batch) {
        return threadLocal.get(batch) == null ? true : false;
    }
}
